using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
	public int speed = 5;
	public float tilesH = 1;//min(1)
	public float tilesV = 1;//min(1)
	public Vector2 startingFrame = new Vector2();
	
	private Sprite playerSprite;//create an instance of the sprite
	
	void Awake()
	{
		playerSprite = new Sprite(tilesH, tilesV, startingFrame);
	}
	
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		Movement();
		
		
	}
	
	// Makes the Ship move
	void Movement()
	{
		float moveVertical = Input.GetAxis("Vertical")*Time.deltaTime*speed;
		float moveHorizontal = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
		
		if(transform.position.x <= -12.5f && transform.position.x >= 12.5f)
		{
			if(transform.position.x <= -12.5f)
			{
				transform.position = new Vector3(-12.0f, transform.position.y, 0);
			}
			else if(transform.position.x >= 12.5f)
			{
				transform.position = new Vector3(12.0f, transform.position.y, 0);
			}
			//transform.position += new Vector3(-moveHorizontal,moveVertical,0f);
		}
		else if(transform.position.x >= -12.5f && transform.position.x <= 12.5f)
		{
			transform.position += new Vector3(-moveHorizontal,moveVertical,0f);
		}
		//Checks if the object is moving horizontally
		if(Input.GetAxis("Horizontal") != 0)
		{
			//check wich side it being pressed and set proper animation
			if(Input.GetKey(KeyCode.LeftArrow))
			{
				playerSprite.SetFrame(this.transform, new Vector2(0,0));
				//renderer.material.SetTextureOffset("_MainTex", new Vector2(0,0)); 
			}
			else if(Input.GetKey(KeyCode.RightArrow))
			{
				playerSprite.SetFrame(this.transform, new Vector2(2,0));
				//renderer.material.SetTextureOffset("_MainTex", new Vector2(0.667f,0));
			}
		}
		else // if there's no movement, change to default animation
		{
			playerSprite.SetFrame(this.transform, new Vector2(1,0));
			//renderer.material.SetTextureOffset("_MainTex", new Vector2(0.331f,0));
		}
	}
}
